export const generateAsyncUpdate = <T = any>(updateFun: (val: T) => void) => {
  let tagNum = 0;
  return async (newVal: () => Promise<T>) => {
    const tag = ++tagNum;
    const value = await newVal();
    const isNew = tagNum === tag;
    if (isNew) updateFun(value);
    return isNew;
  };
};

export async function getModel() {
  const modelData = await (navigator as any)?.userAgentData?.getHighEntropyValues(['model']);
  if (modelData) {
    return (modelData?.model ?? '').trim();
  }
  const userAgent =
    navigator.userAgent.indexOf('Android') !== -1 || navigator.userAgent.indexOf('Adr') !== -1;
  if (userAgent) {
    return (
      navigator.userAgent
        ?.split('(')?.[1]
        ?.split(')')?.[0]
        ?.split('; ')
        ?.pop()
        ?.split(' Build')?.[0] ?? ''
    ).trim();
  }
  return '';
}

export function getBrowserVersion() {
  const Sys: any = {};
  const ua = navigator.userAgent.toLowerCase();
  let s;
  (s = ua.match(/rv:([\d.]+)\) like gecko/))
    ? (Sys.ie = s[1])
    : (s = ua.match(/msie ([\d\.]+)/))
    ? (Sys.ie = s[1])
    : (s = ua.match(/edge\/([\d\.]+)/))
    ? (Sys.edge = s[1])
    : (s = ua.match(/firefox\/([\d\.]+)/))
    ? (Sys.firefox = s[1])
    : (s = ua.match(/(?:opera|opr).([\d\.]+)/))
    ? (Sys.opera = s[1])
    : (s = ua.match(/chrome\/([\d\.]+)/))
    ? (Sys.chrome = s[1])
    : (s = ua.match(/version\/([\d\.]+).*safari/))
    ? (Sys.safari = s[1])
    : 0;
  // 根据关系进行判断
  if (ua.includes('EdgA')) return 'EdgA: ' + Sys.chrome;
  if (Sys.ie) return 'IE: ' + Sys.ie;
  if (Sys.edge) return 'Edge: ' + Sys.edge;
  if (Sys.firefox) return 'Firefox: ' + Sys.firefox;
  if (Sys.chrome) return 'Chrome: ' + Sys.chrome;
  if (Sys.opera) return 'Opera: ' + Sys.opera;
  if (Sys.safari) return 'Safari: ' + Sys.safari;
  return 'Unkonwn';
}

export function getResolution() {
  return `${window.screen.width}x${window.screen.height}`;
}

// 获取操作系统信息
export function getOsInfo() {
  var userAgent = navigator.userAgent.toLowerCase();
  var name = 'Unknown';
  var version = 'Unknown';
  if (userAgent.indexOf('win') > -1) {
    name = 'Windows';
    if (userAgent.indexOf('windows nt 5.0') > -1) {
      version = 'Windows 2000';
    } else if (
      userAgent.indexOf('windows nt 5.1') > -1 ||
      userAgent.indexOf('windows nt 5.2') > -1
    ) {
      version = 'Windows XP';
    } else if (userAgent.indexOf('windows nt 6.0') > -1) {
      version = 'Windows Vista';
    } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
      version = 'Windows 7';
    } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
      version = 'Windows 8';
    } else if (userAgent.indexOf('windows nt 6.3') > -1) {
      version = 'Windows 8.1';
    } else if (
      userAgent.indexOf('windows nt 6.2') > -1 ||
      userAgent.indexOf('windows nt 10.0') > -1
    ) {
      version = 'Windows 10';
    } else {
      version = 'Unknown';
    }
  } else if (userAgent.indexOf('iphone') > -1) {
    name = 'iPhone';
  } else if (userAgent.indexOf('mac') > -1) {
    name = 'Mac';
  } else if (
    userAgent.indexOf('x11') > -1 ||
    userAgent.indexOf('unix') > -1 ||
    userAgent.indexOf('sunname') > -1 ||
    userAgent.indexOf('bsd') > -1
  ) {
    name = 'Unix';
  } else if (userAgent.indexOf('linux') > -1) {
    if (userAgent.indexOf('android') > -1) {
      name = 'Android';
    } else {
      name = 'Linux';
    }
  } else {
    name = 'Unknown';
  }
  return { name, version };
}
